//
//  NSObject+Message.m
//  DS_PJ_01
//
//  Created by admin on 16/8/17.
//  Copyright © 2016年 zhiqiyun. All rights reserved.
//

#import "NSObject+Message.h"
#import "UILabel+Normal.h"

@implementation NSObject(Message)

+ (UILabel *)shareMessageLabel {
    static UILabel *_label = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _label = [[UILabel alloc]initWithFrame:CGRectZero];
        _label.font = [UIFont systemFontOfSize:14];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor = [UIColor whiteColor];
        _label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        _label.layer.cornerRadius = 4.0f;
        _label.layer.masksToBounds = YES;
        
        
    });
    return _label;
}

+(void)showAlertMessage:(NSString *)message
{
    [NSObject showAlertMessage:message time:2 top:sScreenHeight - 100 superView:nil];
}

+(void)showAlertMessage:(NSString *)message time:(CGFloat)time;
{
    [NSObject showAlertMessage:message time:time top:sScreenHeight - 100 superView:nil];
}

+(void)showAlertMessage:(NSString *)message top:(CGFloat)top
{
    [NSObject showAlertMessage:message time:2 top:top superView:nil];
}

+(void)showAlertMessage:(NSString *)message top:(CGFloat)top superView:(UIView *)superView
{
    [NSObject showAlertMessage:message time:2 top:top superView:superView];
}


+(void)showAlertMessage:(NSString *)message time:(CGFloat)time top:(CGFloat)top superView:(UIView *)superView
{
    
    UILabel *dsLabel = [UILabel shareMessageLabel];
    dsLabel.frame = CGRectMake(0, WidthAuto(20), sScreenWith - WidthAuto(40), 100);
    dsLabel.text = message;
    dsLabel.alpha = 1;
    
    CGSize size = [dsLabel compute_limitedWidth];
    
    dsLabel.frame = CGRectMake(sScreenWith/2 - size.width/2, top, size.width + 20, size.height);
    
    if (!superView) {
        superView = [UIApplication sharedApplication].keyWindow;
    }
    
    [superView addSubview:dsLabel];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 animations:^{
            dsLabel.alpha = 0;
        }];
        
    });
}

-(UIViewController *)getCurrentController
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}



@end
